import React, {Component} from 'react';
//引入store
import store from "../../redux/store";
import {createIncrementAction,createDecrementAction,createIncrementAsyncAction} from '../../redux/count_action'

class Index extends Component {
    // componentDidMount() {
    //     // 监测redux中状态的变化，主要变化就调用render
    //     store.subscribe(()=>{
    //         this.01-setState({})
    //     })
    // }

    // state={count:0}
    //加
    increment = () => {
        const {value} = this.selectNumber
        store.dispatch(createIncrementAction(value*1))
    }
    //减
    decrement = () => {
        const {value} = this.selectNumber
        store.dispatch(store.dispatch(createDecrementAction(value*1)))
    }
    //奇数加
    incrementIfOdd = () => {
        const {value} = this.selectNumber
        //获取reducer的值
        const count = store.getState()
        if(count%2!==0){
            store.dispatch(store.dispatch(createIncrementAction(value*1)))
        }
    }
    //异步加
    incrementAsync = () => {
        const {value} = this.selectNumber
        // const count = store.getState()
        // setTimeout(()=>{
            store.dispatch(store.dispatch(createIncrementAsyncAction(value*1,500)))
        // },500)
    }

    render() {
        return (
            <div>
                <h1>当前求和为：{store.getState()}</h1>
                <select ref={c=>{this.selectNumber=c}}>
                    <option value="1">1</option>
                    <option value="2">2</option>
                    <option value="3">3</option>
                </select>
                <button style={{marginLeft:10}} onClick={this.increment}>+</button>
                <button style={{marginLeft:10}} onClick={this.decrement}>-</button>
                <button style={{marginLeft:10}} onClick={this.incrementIfOdd}>当前求和为奇数再加</button>
                <button style={{marginLeft:10}} onClick={this.incrementAsync}>异步加</button>
            </div>
        );
    }
}

export default Index;